//
//  TravelNotesSecondViewController.m
//  ITrip
//
//  Created by dlios on 15-5-22.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TravelNotesSecondViewController.h"
#import "URLPath.h"
#import <AFNetworking.h>
#import "TravelNotesSecondParser.h"
#import "TravelNotesSecondTableViewCell.h"
#import "TravelNotesSecondModel.h"
#import "UIImageView+AFNetworking.h"
#import "UMSocial.h"
#import "Database.h"

@interface TravelNotesSecondViewController ()<UITableViewDataSource, UITableViewDelegate, UMSocialUIDelegate, UIAlertViewDelegate, UIActionSheetDelegate>
{
    UITableView *tView;
    UITableView *GeneralTableView;
    UIImageView *iView;
    
}
@property(nonatomic, retain) NSMutableArray *arrayData;

@property(nonatomic, retain) UIButton *generalBtn;

@property(nonatomic, assign) NSInteger sepLength;

@property(nonatomic, retain) UITapGestureRecognizer *tapGesture;

@property(nonatomic, retain) NSThread *thread;

@property(nonatomic, retain) UIButton *shareBtn;

@property(nonatomic, retain) UIButton *collectBtn;

@end

@implementation TravelNotesSecondViewController

static NSString *identifier = @"cell";
static NSInteger time1 = 0;
- (void)dealloc
{
    [_arrayData release];
    [_generalBtn release];
    [_tapGesture release];
    [_thread release];
    [_shareBtn release];
    [_collectBtn release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];

    // 初始化controller
    self.tabBarController.tabBar.hidden  = YES;
    self.tabBarController.tabBar.translucent = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    UIImage *icon = [UIImage imageNamed:@"iconfont_return"];
    icon = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:icon style:UIBarButtonItemStyleDone target:self action:@selector(getBack)];
    self.arrayData = [NSMutableArray array];
        // 分享按钮
    UIImage *share = [UIImage imageNamed:@"iconfont-gengduo"];
    share = [share imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:share style:UIBarButtonItemStyleDone target:self action:@selector(shareAndCollect)];
    self.shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    UIImage *shareBtn = [UIImage imageNamed:@"iconfont-share"];
    shareBtn = [shareBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.shareBtn setImage:shareBtn forState:UIControlStateNormal];
    
    UIImage *collectBtn = [UIImage imageNamed:@"iconfont-shoucang"];
    collectBtn = [collectBtn imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    [self.collectBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.collectBtn setImage:collectBtn forState:UIControlStateNormal];
    
    self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, -30, 30, 30);
    self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, -30, 30, 30);
    
    self.shareBtn.layer.cornerRadius = 15;
    self.shareBtn.layer.masksToBounds = YES;
    self.collectBtn.layer.cornerRadius = 15;
    self.collectBtn.layer.masksToBounds = YES;
    
    self.shareBtn.alpha = 0.4;
    self.collectBtn.alpha = 0.4;
    
    self.shareBtn.backgroundColor = [UIColor grayColor];
    self.collectBtn.backgroundColor = [UIColor grayColor];
    
    [self.shareBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    [self.collectBtn addTarget:self action:@selector(collect) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self.view addSubview:self.shareBtn];
    [self.view addSubview:self.collectBtn];
    
    // 网络请求
    NSString *path = [URLPath travelNotesSecondURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // foriegn跳转页面过来
    if (self.chooseView == 1) {
        path = @"http://chanyouji.com/api/plans/";
    } else {
        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    NSString *str = [path stringByAppendingFormat:@"%ld.json", self.number];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.arrayData = [TravelNotesSecondParser getParser:data];
        [self tableViewDidLoad];
        [activityIndicator stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];
    
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1) {
        return;
    }
    // 定义菊花控件
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:activityIndicator];
    activityIndicator.color = [UIColor grayColor];
    activityIndicator.center = self.view.center;
    [activityIndicator release];
    [activityIndicator startAnimating];
    
    NSString *path = [URLPath travelNotesSecondURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    // foriegn跳转页面过来
    if (self.chooseView == 1) {
        path = @"http://chanyouji.com/api/plans/";
    } else {
        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    }
    
    NSString *str = [path stringByAppendingFormat:@"%ld.json", self.number];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager.responseSerializer setAcceptableContentTypes: [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", nil]];
    
    
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        self.arrayData = [TravelNotesSecondParser getParser:data];
        [self tableViewDidLoad];
        [activityIndicator stopAnimating];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [activityIndicator stopAnimating];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络连接失败" message:nil delegate:self cancelButtonTitle:@"重新连接" otherButtonTitles:nil, nil];
        alert.delegate = self;
        [alert show];
    }];

}


#pragma -mark share
- (void)shareAndCollect
{
    if (time1 == 0) {
        [UIView animateWithDuration:0.5 animations:^{
            self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, 0, 50, 50);
            self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, 50, 50, 50);
            self.shareBtn.layer.cornerRadius = 25;
            self.shareBtn.layer.masksToBounds = YES;
            self.collectBtn.layer.cornerRadius = 25;
            self.collectBtn.layer.masksToBounds = YES;
            
        }];
        time1 = 1;
    } else {
        [UIView animateWithDuration:0.5 animations:^{
            self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 40, 40);
            self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 40, 40);
            
        }];
        time1 = 0;
    }
    
    [self.view bringSubviewToFront:self.shareBtn];
    [self.view bringSubviewToFront:self.collectBtn];
}


- (void)share
{
    [UIView animateWithDuration:0.5 animations:^{
        self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 30, 30);
        self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 30, 30);
        self.shareBtn.layer.cornerRadius = 15;
        self.shareBtn.layer.masksToBounds = YES;
        self.collectBtn.layer.cornerRadius = 15;
        self.collectBtn.layer.masksToBounds = YES;
    }];
    
    NSArray *arra = [self.arrayData firstObject];
    NSArray *arr = [arra objectAtIndex:0];
    TravelNotesSecondModel *travelNotesModel = [arr firstObject];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:travelNotesModel.front_cover_photo_url] options:NSDataReadingMappedAlways error:nil];
    UIImage *image = [UIImage imageWithData:data];
    [UMSocialSnsService presentSnsIconSheetView:self
                                                  appKey:@"5569046f67e58e40e6002b92"
                                               shareText:travelNotesModel.name
                                              shareImage:image
                                         shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
                                                delegate:self];
}
- (void)collect
{
    [UIView animateWithDuration:0.5 animations:^{
        self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 30, 30);
        self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 30, 30);
        self.shareBtn.layer.cornerRadius = 15;
        self.shareBtn.layer.masksToBounds = YES;
        self.collectBtn.layer.cornerRadius = 15;
        self.collectBtn.layer.masksToBounds = YES;
    }];
    NSArray *array = [Database selectData];
    int i = 0;
    NSArray *arra = [self.arrayData firstObject];
    NSArray *arr = [arra objectAtIndex:0];
    TravelNotesSecondModel *travelNotesModel = [arr firstObject];
    
    for (DbModel *model in array) {
        if (model.titleId == self.number) {
            i = 1;
            break;
        }
    }
        if (i == 1) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"您已经收藏过了!" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil , nil];
            alert.tag = 1;
            [alert show];
        } else {
            DbModel *model = [DbModel getDbModel];
            model.title = travelNotesModel.name;
            model.titleId = self.number;
            model.choose = 0;
            model.imageURL = travelNotesModel.front_cover_photo_url;
            [Database insertValue:model];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"收藏成功!" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil , nil];
            alert.tag = 1;
            [alert show];
        }
    
    
}

#pragma -mark 隐藏目录
- (void)beginTouch
{
    [UIView animateWithDuration:0.5 animations:^{
        self.generalBtn.frame = CGRectMake(20, self.view.frame.size.height - 50, 40, 40);
        GeneralTableView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
    }];
    [UIView animateWithDuration:0.5 animations:^{
        self.shareBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 40, 40);
        self.collectBtn.frame = CGRectMake(self.view.frame.size.width - 50, -40, 40, 40);
    }];
//    self.tapGesture.enabled = NO;
    
}

- (void)tableViewDidLoad
{
     tView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:tView];
    [tView release];
    tView.delegate = self;
    tView.dataSource = self;
    tView.tag = 1;
    
    // 添加点击
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(beginTouch)];
    [tView addGestureRecognizer:self.tapGesture];
    self.tapGesture.enabled = YES;
    
    // 呼出概况栏
    self.generalBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.generalBtn.backgroundColor = [UIColor clearColor];
    self.generalBtn.frame = CGRectMake(20, self.view.frame.size.height - 50, 40, 40);
    [self.view addSubview:self.generalBtn];
    [self.view bringSubviewToFront:self.generalBtn];
    [self.generalBtn addTarget:self action:@selector(getGeneral) forControlEvents:UIControlEventTouchUpInside];
    self.generalBtn.layer.cornerRadius = 20;
    self.generalBtn.layer.masksToBounds = YES;
    self.generalBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    self.generalBtn.layer.borderWidth = 2;
    [self.generalBtn setImage:[UIImage imageNamed:@"icon_list"] forState:UIControlStateNormal];
    
    
    // 添加第二个tableview
    GeneralTableView = [[UITableView alloc] initWithFrame:CGRectMake(-200, 0, 200, self.view.frame.size.height) style:UITableViewStylePlain];
    [self.view addSubview:GeneralTableView];
    [GeneralTableView release];
    GeneralTableView.alpha = 0.8;
    GeneralTableView.backgroundColor = [UIColor blackColor];
    GeneralTableView.delegate = self;
    GeneralTableView.dataSource = self;
    GeneralTableView.tag = 2;
    GeneralTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    GeneralTableView.hidden = YES;
    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 240)];
    NSArray *array = [self.arrayData firstObject];
    NSArray *arr = [array objectAtIndex:0];

    
    TravelNotesSecondModel *model = [arr firstObject];
    
    iView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headerView.frame.size.width, headerView.frame.size.height)];
    [iView setImageWithURL:[NSURL URLWithString:model.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"defaultPic"]];
//    [iView sd_setImageWithURL:[NSURL URLWithString:model.front_cover_photo_url] placeholderImage:[UIImage imageNamed:@"defaultPic"] options:SDWebImageProgressiveDownload completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        
//    }];
    
//    iView.autoresizingMask = UIViewAutoresizingFlexibleHeight| UIViewAutoresizingFlexibleWidth;
//    iView.clipsToBounds = YES;
//    iView.contentMode = UIViewContentModeScaleAspectFill;
//    
//     _header = [CExpandHeader expandWithScrollView:tView expandView:headerView];
    tView.tableHeaderView = headerView;
    UIImageView *userImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 160, 40, 40)];
    [userImage setImageWithURL:[NSURL URLWithString:model.userImage] placeholderImage:nil];
    [headerView addSubview:iView];
    [headerView addSubview:userImage];
    userImage.layer.cornerRadius = 20;
    userImage.layer.masksToBounds = YES;
    userImage.layer.borderColor = [UIColor whiteColor].CGColor;
    userImage.layer.borderWidth = 2;
    UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake(70, 150, 240, 30)];
    userName.text = model.name;
    userName.textColor = [UIColor whiteColor];
    userName.font = [UIFont boldSystemFontOfSize:17];
    [headerView addSubview:userName];
    
    UILabel *date = [[UILabel alloc] initWithFrame:CGRectMake(70, 180, 280, 30)];
    date.text = [[model.start_date stringByAppendingFormat:@" | %ld", self.days] stringByAppendingFormat:@"天, %ld图", model.photos_count];
    date.textColor = [UIColor whiteColor];
    date.font = [UIFont boldSystemFontOfSize:15];
    [headerView addSubview:date];
    
    
    [date release];
    [userName release];
    [iView release];
    [headerView release];
    
}
// 获取第二个tableView
- (void)getGeneral
{
    GeneralTableView.hidden = NO;
    self.tapGesture.enabled = YES;
    [UIView animateWithDuration:0.5 animations:^{
        GeneralTableView.frame = CGRectMake(0, 0, 200, self.view.frame.size.height);
        self.generalBtn.frame = CGRectMake(20, self.view.frame.size.height, 40, 40);
    }];
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag == 1) {
        TravelNotesSecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[TravelNotesSecondTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
            cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, self.view.frame.size.width, cell.frame.size.height);
        }
        
        cell.titleView.layer.borderColor = nil;
        cell.titleView.layer.borderWidth = 0;
        cell.url.image = nil;
        cell.name_zh_cn.text = nil;
        cell.titleView.image = nil;
        cell.descrip.text = nil;
        cell.titleView.backgroundColor = nil;
        cell.comment.text = nil;
        
        
        NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
        NSArray *arr = [array objectAtIndex:indexPath.row];
        TravelNotesSecondModel *model = [arr objectAtIndex:2];
        
        [cell setTravelNotesSecondModel:model];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    } else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
        }
        cell.backgroundColor = [UIColor blackColor];
        NSArray *array = [self.arrayData lastObject];
        NSArray *arr = [array objectAtIndex:indexPath.section];
//        TravelNotesSecondModel *model1 = [arr firstObject];
//        NSLog(@"%ld", model1.day);
        TravelNotesSecondModel *model = [TravelNotesSecondModel getModel];
        if (indexPath.row == 0) {
            if (arr.count > 0) {
                model = [arr objectAtIndex:indexPath.row];
            }
        } else {
            if (arr.count > 0) {
                model = [arr objectAtIndex:indexPath.row - 1];
            }
        }
        
        if (indexPath.row == 0) {
            
            cell.textLabel.textColor = [UIColor whiteColor];
            cell.textLabel.text = [NSString stringWithFormat:@"o DAY%ld", model.day];
//            NSLog(@"%ld", model.day);
            
        } else {
            cell.textLabel.text = [NSString stringWithFormat:@"o  %@", model.entry_name];
            cell.textLabel.textColor = [UIColor whiteColor];
        }
        if (array.count > indexPath.section) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(20, 35, 1, 15)];
            view.backgroundColor = [UIColor whiteColor];
            [cell addSubview:view];
            [view release];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 1) {
        return 50;
    } else {
        return 10;
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView.tag == 1) {
        UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width - 60, 50)]autorelease];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 70, 50)];
        [view addSubview:dayLabel];
        [dayLabel release];
        UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 0, 120, 50)];
        [view addSubview:dateLabel];
        dayLabel.textColor = [UIColor blackColor];
        dateLabel.textColor = [UIColor blackColor];
        [dateLabel release];
        NSArray *array = [self.arrayData objectAtIndex:section];
        NSArray *arr = [array firstObject];
        TravelNotesSecondModel *model = [arr objectAtIndex:2];
        dayLabel.text = [NSString stringWithFormat:@"DAY%ld", model.day];
        dayLabel.font = [UIFont boldSystemFontOfSize:20];
        
        if (model.trip_date != nil && ![model.trip_date isEqual:[NSNull null]]) {
            dateLabel.text = [NSString stringWithString:model.trip_date];
        }
        return view;
    } else {
        return nil;
    }
}

#pragma -mark cell 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag == 2) {
        NSArray *b = [self.arrayData lastObject];
        NSArray *br = [b objectAtIndex:indexPath.section];
        TravelNotesSecondModel *mo = [TravelNotesSecondModel getModel];
        if (indexPath.row == 0) {
            mo = [br objectAtIndex:indexPath.row];
        } else {
            mo = [br objectAtIndex:indexPath.row - 1];
        }
        //        NSLog(@"%@", mo.entry_name);
        NSArray *a = [self.arrayData objectAtIndex:indexPath.section];
        int i = 0;
        for (NSArray *ar in a) {
            TravelNotesSecondModel *model = [ar objectAtIndex:2];
            //            NSLog(@"%@", model.entry_name);
            if (model.entry_name == nil || [model.entry_name isEqual:[NSNull null]] || ![model.entry_name isEqualToString:mo.entry_name]) {
                i++;
            } else {
                break;
            }
        }
        
        if (indexPath.row == 0) {
            NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
            [tView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [UIView animateWithDuration:0.5 animations:^{
                self.generalBtn.frame = CGRectMake(20, self.view.frame.size.height - 50, 40, 40);
                GeneralTableView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
            }];
            
        } else {
            NSIndexPath *path = [NSIndexPath indexPathForRow:i inSection:indexPath.section];
            [tView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES];
            [UIView animateWithDuration:0.5 animations:^{
                self.generalBtn.frame = CGRectMake(20, self.view.frame.size.height - 50, 40, 40);
                GeneralTableView.frame = CGRectMake(-200, 0, 200, self.view.frame.size.height);
            }];
        }
    }
    
//    } else {
//        NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
//        NSArray *arr = [array objectAtIndex:indexPath.row];
//        TravelNotesSecondModel *model = [arr objectAtIndex:2];
//        if (model.url != nil && ![model.url isEqual:[NSNull null]]) {
//            UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"保存图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [action showFromTabBar:self.tabBarController.tabBar];
//            self.myPhoto = model.url;
//        }
//    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
    } else {
        
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

//    NSLog(@"%ld", tableView.tag);
    if (tableView.tag == 1) {
        NSArray *array = [self.arrayData objectAtIndex:section];
//        NSLog(@"%ld", array.count);
        return array.count;
    } else {
        NSArray *array = [self.arrayData objectAtIndex:section];
        NSArray *arr = [array lastObject];
        TravelNotesSecondModel *model = [arr objectAtIndex:1];
        if (model.num != 0) {
            return model.num + 1;
        } else {
            return 1;
        }
        
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arrayData.count - 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView.tag == 1) {
        NSArray *array = [self.arrayData objectAtIndex:indexPath.section];
        NSArray *arr = [array objectAtIndex:indexPath.row];
        
        TravelNotesSecondModel *model = [arr objectAtIndex:2];
//        NSLog(@"%ld, %ld, %@", indexPath.section, indexPath.row, model.entry_type);
        
        // 动态计算label高度
        NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName];
        CGRect frame;
        if (model.descrip != nil && ![model.descrip isEqual:[NSNull null]]) {
            frame = [model.descrip boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
        }
        
        
        if (model.entry_type == nil || [model.entry_type isEqual:[NSNull null]]) {// 无title
            if ((model.url == nil || [model.url isEqual:[NSNull null]]) && (model.descrip == nil || [model.descrip isEqual:[NSNull null]])) {
                // 都没有
                return 0;
            } else if (model.url != nil && ![model.url isEqual:[NSNull null]] && (model.descrip == nil || [model.descrip isEqual:[NSNull null]])) {
                // 有图无文字
                //            NSLog(@"%f", ((self.view.frame.size.width - 40) * (model.image_height / (CGFloat)model.image_width)));
                return ((self.view.frame.size.width - 40) * (model.image_height / (CGFloat)model.image_width)) + 40;
                
            } else if (model.url != nil && ![model.url isEqual:[NSNull null]] && model.descrip != nil && ![model.descrip isEqual:[NSNull null]]) {
                // 有图有文字
                //            NSLog(@"%f", (frame.size.height + ((self.view.frame.size.width - 40) * (model.image_height / (CGFloat)model.image_width))));
                return (frame.size.height + ((self.view.frame.size.width - 20) * (model.image_height / (CGFloat)model.image_width))) + 30;
            } else {
                // 纯文字
                //            NSLog(@"%f", frame.size.height + 60);
                //                NSLog(@"1");
                //                NSLog(@"%f", frame.size.height);
                return frame.size.height + 20;
            }
            
        } else if (model.entry_type != nil && ![model.entry_type isEqual:[NSNull null]]) {
            // 有title
            if ((model.url == nil || [model.url isEqual:[NSNull null]]) && (model.descrip == nil ||[model.descrip isEqual:[NSNull null]])) {
                // 无图无文字
                return 80;
            } else if (model.url != nil && ![model.url isEqual:[NSNull null]] && ((model.descrip == nil || [model.descrip isEqual:[NSNull null]]))) {
                // 有图无文字
                //            NSLog(@"%f", ((self.view.frame.size.width - 40) * (model.image_height / (CGFloat)model.image_width)));
                return 70 + ((self.view.frame.size.width - 20) * (model.image_height / (CGFloat)model.image_width));
                
            } else if (model.url != nil && ![model.url isEqual:[NSNull null]] && model.descrip != nil && ![model.descrip isEqual:[NSNull null]]) {
                // 有图有文字
                return 90 + (frame.size.height + ((self.view.frame.size.width - 20) * (model.image_height / (CGFloat)model.image_width)));
                
            } else if ((model.url == nil || [model.url isEqual:[NSNull null]]) && model.descrip != nil && ![model.descrip isEqual:[NSNull null]]){
                // 无图有文字
                return 50 + frame.size.height + 20;
            }
        }
    } else if (tableView.tag == 2) {
        return 50;
    }
    return 0;
}

- (void)getBack
{
    self.generalBtn.hidden = YES;
    GeneralTableView.hidden = YES;
    if (self.isJourney != 1) {
        self.tabBarController.tabBar.hidden = NO;
        self.tabBarController.tabBar.translucent = NO;
    }
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
